<?php
namespace app\common\model;
use think\Model;
use app\common\model\Redis;
class Dev_data_info extends Model
{
	/**
     * 删
     * @param $where  条件
     * @return int
     */
    public function del($where){
        return self::where($where)->delete();
    }

	/**
	 * 获取单条设备信息
	 * @param  [array] $where [数组]
	 * @return [type]        [description]
	 */
	public function getOne($where,$field='*'){
        $result = self::field($field)->where($where)->find();
        if($result){
            $result = $result->getData();
        }
        return $result;
	}

    /**
     * 获取列表
     * @param int $end 结束时间
     * @return array
     * */
    public function getAll($where=[],$field="*"){
        $result = self::field($field);
        
        if ($where){
            $result->where($where);
        }
        $result = $result->select();

        return $result;
    }

    /**
     * 设备号参数id是否是积累型数据
     * @param  [string] $devid [设备号]
     * @param  [string] $did   [操作id]
     * @return [string] $if_his [1:是 0：否]
     */
    public function if_his($devid,$did){

        $redis = new Redis();
        $redis->select(1);

        $res = $redis->lranges($devid."_his_para");
        if(in_array($did,$res)){
             $if_his = 1;
        }else{
             $if_his = 0;
        }

        return $if_his;
    }

    /**
     * 设备号_para_info  获取单位
     * @param  [string] $devid [设备号]
     * @param  [string] $did   [操作id]
     * @return [string] $res   [单位]
     */
    public function units($devid,$did){
        $redis = new Redis();
        $redis->select(1);
        $res = $redis->hget($devid."_para_info",$did);

        if(!$res){
            $where["d.guid1"] = $devid;
            $where["d.d_id"] = $did;
            $info = self::alias('d')->join("unit_table AS u ON(d.unit_id=u.unit_id)","left")->where($where)->field("u.val")->find();
            
			if($info){
	            $info = $info->getData();

                $redis->hset($devid."_para_info",$did,$info["val"]);
    	        $res = $info["val"];
	        }else{
	        	$res = false;
	        }
        }

        return $res;
    }
}